<?

//	*****************************************
//	SbSE DB multiple rows insert component
//	v1.1.3 / 2012.04.27
//	*****************************************
// oci8 only

require_once E.'_lib/_db.php';

class SAVEMETA {

	var $tables;	// into
	var $data;		// data
	var $id;		// insert id
	
	var $res;				// код ошибки	

	var $err = array (
		0 => 'success',
		1 => 'empty result',
		255 => 'not initialized'
	);
	
	function SAVEMETA() {

		$this->options = array();

		$this->datas = '';
		$this->data = 0;
		$this->id = 0;

		$this->ai_field = 0;

		$this->db = false;
		

		$this->res = 255;
	}

	function init( $datas, $db = false ) {
	
		if ($datas)
			$this->datas = $datas;
		else
			return 0;	// error 255 - not initialized.
			
		if (defined('USE_ADODB'))
			if (!$db && isset($GLOBALS['db']))
				$db = $GLOBALS['db'];
		
		$this->db = $db;

// magic		
		$this->table = tb('data');
		$this->xtable = tb('xdata');
		$this->ai_field = 'id';
		$this->seq = T.'data_seq';

		$this->clob = 'value';
		$this->filtered = 'filtered';
		$this->flag = 'large';



		$this->res = $this->fetch();

		return ! (bool) $this->res;
	}


	function storeRec($rec, $seq = false) {	// store single record with AI (by sequencer $seq) & CLOB processing

		if (defined('USE_ADODB')) {

			if ($seq && !$rec[$this->ai_field])
				$rec[$this->ai_field] = $id = $this->db->GenID($seq);
			else
				$id = $rec[$this->ai_field];
		}
			

		if ($f = $rec[$this->flag]) {
			$value = $rec[$this->clob];
			$filtered = html2text($value);
			
			$rec[$this->clob] = $value ? CLOB_SPACER : CLOB_EMPTY;
		}
		unset($rec[$this->flag]);
 

		if (defined('USE_ADODB'))
			$this->db->AutoExecute($this->table, $rec, 'INSERT');
		else
			$id = datafetch('db_insert', $this->table, $rec);


		if ($f && $value && $id)
		
			if (defined('USE_ADODB')) {
	
				if (defined('USE_ADODB_OCI') && (strlen($value) >= 4000)) {	// oracle
					$xrec = array('id' => $id, $this->clob => '', $this->filtered => '');
					$this->db->AutoExecute($this->xtable, $xrec, 'INSERT');
	
					$this->db->UpdateClob($this->xtable, $this->clob, $value, 'id='.$id);
					$this->db->UpdateClob($this->xtable, $this->filtered, $filtered, 'id='.$id);
					
				} else {	// adodb
					
					$xrec = array('id' => $id, $this->clob => $value, $this->filtered => $filtered);
					$this->db->AutoExecute($this->xtable, $xrec, 'INSERT');
				}
	
			} else {	// mysql
				dataset('db_insert', $this->xtable, array('id' => $id, $this->clob => $value, $this->filtered => $filtered));
			}
		
		return $id;
	}

	function fetch() {		// ! надо проверять, передали массив значений или строк - и обрабатывать соответственно.

			if (isset($this->datas[0])) {

				foreach($this->datas as $rec)
					$this->id = $this->storeRec($rec, $this->seq);	// store multiple records
	
			} else
	
				$this->id = $this->storeRec($this->datas, $this->seq);	// store single record

		$this->data = $this->id;

		return 0;		// ok
	}
	

	function error() { return $this->err[$this->res]; }

}